package com.github.ldw4033.spring.controller.resolver;

import com.github.ldw4033.spring.exception.CustomException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by liudw on 2015/8/2 0002.
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        CustomException customException=null;
        if(ex instanceof CustomException){

            customException=(CustomException)ex;
        }else{
            customException=new CustomException("未知异常,请与管理员联系");
        }

        ModelAndView mv=new ModelAndView();
        mv.addObject("message",customException.getMessage());
        mv.setViewName("error");
        return mv;
    }
}
